package sxe.core.math;
/*
Created on Jun 17, 2005
(C) 2005 
@author Michael Long
*/

import snobject.snDouble;
import snobject.snObject;
import snobject.snObjectArgList;
import step.FunctionWrapper;
import step.EvaluationFailure;
import sxe.core.EvaluationFailureReasons;

/**
 * Returns the division of a set of values
 * @param argv must contain snDouble objects in the stack
 * @return returns the division of the arguments
 */

public class divide extends FunctionWrapper{
	
	public snObject Call(snObjectArgList argv) throws EvaluationFailure{
		// REQUIRES: argv must contain snDouble objects
		// EFFECTS: if trying to divide by zero, throw EvaluationFailure exception 
		//			otherwise returns the snDouble of the division of the values from argv
		double ret = 0;
		int index = argv.index();
				
		ret = ((snDouble)argv.objectAt(index)).getDouble();
		
		while(argv.hasNext()){
			double temp = argv.popDouble().getDouble();
			if(temp == 0)
				throw new EvaluationFailure(super.expnode,
						EvaluationFailureReasons.DIVIDE_BY_ZERO);
			ret /= temp;
		}

		return new snDouble(new Double(ret));
	}
}
